<template>
  <div class="app-container">
    <el-card>
      <el-table :data="list">
        <el-table-column
          v-for="item in listC"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column
          label="操作"
          width="300px"
          v-slot="{ row, row: { applyStatus, approveStatus } }"
        >
          <el-button
            size="mini"
            type="success"
            v-if="applyStatus == '已审核' && approveStatus == '通过'"
            @click="genPurchaseRequest(row)"
            >生成采购申请</el-button
          >
          <el-button size="mini" type="primary" @click="handleEdit(row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            v-if="
              (applyStatus == '已审核' && approveStatus == '驳回') ||
              applyStatus == '未提交'
            "
            @click="delReplenishmentApplication(row.id)"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      center
      :visible.sync="visible"
      title="修改补货申请"
      @close="close"
    >
      <el-form ref="listEFRef" :model="listEF">
        <el-form-item label="备注" prop="remark">
          <el-input
            :disabled="disable"
            type="textarea"
            :rows="3"
            resize="none"
            v-model="listEF.remark"
            placeholder="备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-table :data="listEF.detailList">
        <el-table-column v-slot="{ row, $index }" label="物料">
          {{ row.material.name }}
          <el-button
            :disabled="disable"
            size="mini"
            type="primary"
            @click="selectMaterial($index)"
            >选择</el-button
          >
        </el-table-column>
        <el-table-column v-slot="{ row }" label="补货数量">
          <el-input
            :disabled="disable"
            v-model="row.quantity"
            placeholder="补货数量"
          ></el-input>
        </el-table-column>
        <el-table-column v-slot="{ row }" label="备注">
          <el-input
            :disabled="disable"
            v-model="row.remark"
            placeholder="备注"
          ></el-input>
        </el-table-column>
        <el-table-column label="操作" v-slot="{ $index, row }">
          <el-button
            :disabled="disable"
            size="mini"
            type="success"
            @click="addRow($index, row)"
            >添加</el-button
          >
          <el-button
            :disabled="disable"
            size="mini"
            type="mini"
            @click="delMaterial($index)"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <template #footer>
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </template>
    </el-dialog>
    <el-dialog
      :visible.sync="visible2"
      title="选择物料"
      @close="visible2 = false"
    >
      <el-table :data="materialList">
        <el-table-column
          v-for="item in materialC"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column label="选择" v-slot="{ row }">
          <el-button type="primary" @click="changeMaterial(row)"
            >选择</el-button
          >
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { delReplenishmentApplication, genPurchaseRequest, getReplenishmentApplicationList, editReplenishmentApplication } from '@/api_zc/mes/replenishment-application'
import { getMaterialList } from '@/api_zc/mes/product-modeling'
export default {
  mounted () {
    this.getList()
    this.getUnityList()
  },
  data () {
    return {
      list: [],
      pager: {
        page: 1,
        limit: 10,
        total: 0,
      },
      listC: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '单据号',
          prop: 'documentNo'
        },
        {
          label: '状态',
          prop: 'status'
        },
        {
          label: '申请人',
          prop: 'applyUser.realName'
        },
        {
          label: '申请日期',
          prop: 'applyTime'
        },
        {
          label: '申请状态',
          prop: 'applyStatus'
        },
        {
          label: '审核人',
          prop: 'approveUser.realName'
        },
        {
          label: '审核日期',
          prop: 'approveDate'
        },
        {
          label: '审核状态',
          prop: 'approveStatus'
        },
      ],
      listEF: {
        detailList: []
      },
      disable: false,
      visible: false,
      visible2: false,
      materialList: [],
      detailListId: 0,
      materialC: [
        {
          label: '物料名称',
          prop: 'name'
        },
        {
          label: '型号',
          prop: 'model'
        },
      ],
      isFourFive: false
    }
  },
  methods: {
    async getUnityList () {
      const { data: { content } } = await getMaterialList({ pid: 0 })
      this.materialList = content
    },
    async getList () {
      const { data: { total, content } } = await getReplenishmentApplicationList({ page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    handleSizeChange (val) {
      this.pager.limit = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.pager.page = val
      this.getList()
    },
    handleEdit (row) {
      const { applyStatus, approveStatus } = row
      if ((applyStatus == '已审核' && approveStatus == '驳回') || applyStatus == '未提交') {
        // 可编辑
        this.disable = false
        this.isFourFive = true
      } else {
        this.disable = true
        this.isFourFive = false
      }
      this.visible = true
      this.$nextTick(() => {
        this.listEF = row
      })
    },
    async submitEdit () {
      await editReplenishmentApplication(this.listEF)
      this.$message.success('修改成功')
      this.visible = false
      this.getList()
    },
    save () {
      if (this.isFourFive) {
        this.listEF.applyStatus = '未提交'
      }
      this.submitEdit()
    },
    submit () {
      if (this.isFourFive) {
        this.listEF.approveStatus = '待审核'
      }
      this.submitEdit()
    },
    close () {
      this.visible = false
      this.getList()
    },
    selectMaterial (id) {
      this.detailListId = id
      this.visible2 = true
    },
    changeMaterial (row) {
      row.category = ''
      this.listEF.detailList.find((item, index) => {
        return index == this.detailListId
      }).material = row
      this.visible2 = false
    },
    addRow (index, row) {
      const data = JSON.parse(JSON.stringify(row))
      Object.keys(data).forEach(key => {
        if (key == 'material') {
          data[key] = {}
        } else {
          data[key] = ''
        }
      })
      this.listEF.detailList.splice(index + 1, 0, data)
    },
    delMaterial (index) {
      this.listEF.detailList.splice(index, 1)
    },
    delReplenishmentApplication (id) {
      this.$confirm(`是否确认删除补货申请编号为“${id}”的数据项`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delReplenishmentApplication({ ids: [id] })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    genPurchaseRequest (row) {
      this.$confirm(`是否确认生成采购申请清单？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await genPurchaseRequest({ id: row.id })
        this.$message({
          type: 'success',
          message: '生成成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>
</style>